package netTracker.network;

import netTracker.NetTracker;
import netTracker.Util;

/**
 * A message handler for messages specifically sent to a client from a session server.
 * @author David Cummings
 */
public class ClientMessageHandler implements MsgHandler {
	/** Utility class with debug flag: */
	Util util = new Util(this, true);
	
	Message m;
	NetTracker netTracker;
	
	public ClientMessageHandler(NetTracker netTracker){
		this.netTracker = netTracker;
	}
	
	public void handleMsg(Message m) throws PostalException {
		this.m = m;
		
		if(m.getTag().equals("OUTLINE")){
			String[] coordinates = m.getMsg().split(",");
			outline(coordinates, m.getNumber());
		}else if(m.getTag().equals("SONGDATA")){
			netTracker.setSong(m.getSong());
		}
	
	}
	
	public void outline(String[] coordinates, int outlineID){
		try{
			netTracker.outline(outlineID, Integer.parseInt(coordinates[0]), Integer.parseInt(coordinates[1]));
		}catch(NumberFormatException x){
			util.debug("Invalid coordinates to outline. "+x);
		}
	}
}
